<?php
namespace app\wechat\controller;

use Jssdk\JSSDK;
use think\View;

class Index extends Common
{
    public function index()
    {
        /**
         * 如果是微信  需要去获取openid
         */
        if( is_weixin() ){
            if (input('?param.openid')) {
                session('openid', input('param.openid'));
            }
            if (!session('?openid')) {
                $url = config('new.interface_url') . 'wechat/wechat_oauth/index/scope/snsapi_base';
                exit('<script>window.location.href="'.$url.'"</script>');
            }
        }
        /*PJ非微信本地测试方法*/
        $where = ' m.id="'. session('member_id') . '" ';
        $member = db('member')->alias('m')->field('m.*, mr.nickname, mr.headimgurl, mr.phone as phone0')
            ->join("mob_member_role_info mr", "m.id = mr.member_id and mr.type = 0", 'LEFT')
            ->where($where)->find();

        $server_items = db('server_items')->where("display",1)->select();
        $member["server_items"] = $server_items;

        $this->assign('member', $member);//{$member.proposer}{$member.phone}

        return view();
    }

    //用户查看地图维修店
    public function get_data()
    {
        $param = [
            'type' => input('param.type',0),
            'lng' => input('param.lng',0),
            'lat' => input('param.lat',0),
            'zoom' => input('param.zoom',0)
        ];
        $data = call_interface('index_get_data', $param);
        return $data;

    }

    //用户查看附近维修店列表
    public function get_data_list()
    {
        $param = [
            'lng' => input('param.lng',0),
            'lat' => input('param.lat',0),
            'zoom' => input('param.zoom',0)
        ];
        $data = call_interface('index_get_data_list', $param);
        return $data;

    }

    //用户下单
    public function up_data(){

        $param["member_id"] = session('member_id');
        $param["repair_id"] = intval(input('param.repair_id'));
        $param["service_time"] = intval(strtotime(input('param.server_time')));
        $param["service_mode"] = intval(input('param.service_mode'));
        $param["msg"] = input('param.msg');
        $param["address"] = input('param.address');
        $param["apply_person"] = input('param.apply_person');
        $param["phone"] = input('param.phone');
        $param["type"] = intval(input('param.type'));
        $param["lon"] = floatval(input('param.lng'));
        $param["lat"] = floatval(input('param.lat'));
        $param["province"] = intval(input('param.province'));
        $param["city"] = intval(input('param.city'));
        $param["area"] = intval(input('param.area'));
        $param["street"] = input('param.street');
        $param["img1"] = input('param.img1');
        $param["img2"] = input('param.img2');
        $param["img3"] = input('param.img3');
        $param["img4"] = input('param.img4');
        $param["img5"] = input('param.img5');
        $param["source"] = 'web';
        //print_r($param);exit();
        $data = call_interface('order_server', $param);
        return json($data);

    }


    public function index_list(){
        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $where = ' m.id="'. session('member_id') . '" ';
        $member = db('member')->alias('m')->field('m.*, mr.nickname, mr.headimgurl, mr.phone as phone0')
            ->join("mob_member_role_info mr", "m.id = mr.member_id and mr.type = 0", 'LEFT')
            ->where($where)->find();

        $this->assign('member', $member);//{$member.proposer}{$member.phone}

        return view();
    }

    //用户查看维修店详情
    public function index_repair_info(){

        /*PJ非微信本地测试方法*/
        if( is_weixin() ){
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
            $this->assign('signPackage', $signPackage);
        }

        $param = [
            'repair_id' => input('param.id',0)
        ];
        $repair = call_interface('index_repair_info', $param);
        $this->assign('repair', $repair["data"]);

        $where = ' m.id="'. session('member_id') . '" ';
        $member = db('member')->alias('m')->field('m.*, mr.nickname, mr.headimgurl, mr.phone as phone0')
            ->join("mob_member_role_info mr", "m.id = mr.member_id and mr.type = 0", 'LEFT')
            ->where($where)->find();

        $get_lat = floatval(input('param.lat',0));
        $get_lng = floatval(input('param.lng',0));
        $member["get_lat"] = $get_lat;
        $member["get_lng"] = $get_lng;

        $this->assign('member', $member);//{$member.proposer}{$member.phone}

        return view();
    }


    // 获取评论列表
    public function get_comment_list() {
        if (request()->isAjax()) {
            // 获取标签数据
            $repair_id = input('param.repair_id');
            $param['type'] = 2;
            $param['merchant_id'] = $repair_id;
            $data = call_interface('get_comment_list', $param);
            $view = new View();
            $view->assign('data', $data["data"]["data"]);

            $tpl = $view->fetch('index/index_repair_info_comment_tpl');
            //$data = $data->toArray();
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view('base/error');
    }



}
